package ec.cd.common;

import java.nio.channels.SocketChannel;
import java.nio.ByteBuffer;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;

import static ec.cd.common.Sensors.SensorConfig;
import static ec.cd.common.Sensors.SensorData;
import static ec.cd.common.Sensors.SensorRunningMode;

public class Networking {
    public static enum MessageType {
        ClientHello,
        ServerCalibrationRun,
        ServerNormalRun,
        ServerTestingRun,
        ServerReplayingRun,
        ClientSendData,
        ClientBye
    }

    public static enum BodyLocation {
        Torso, Arm, Leg,
    }

    public static class Message implements Serializable {
        public MessageType mt;
        public Object payload;
    }

    public static class ClientHello implements Serializable {
        public BodyLocation bl;
    }

    public static class ServerCalibrationRun implements Serializable {
        public int testLength;

        // Contexts that the user needs to perform
        public String[] phaseNames;
    }

    public static class ServerNormalRun implements Serializable {
        public SensorConfig[] scs;
        public SensorRunningMode srm;
        public int sending_rate;
    }

    public static class ServerTestingRun implements Serializable {
        public boolean enableNetworking;
        public int sending_rate;
        public SensorConfig sc;
    }

    // ServerReplayingRun is the same as ServerNormalRun

    public static class ClientSendData implements Serializable {
        public SensorData[] sensors_data;
    }

    public static void sendMessage(ObjectOutputStream objObjectOutputStream, Message msg) throws IOException{
        objObjectOutputStream.writeObject(msg);
        objObjectOutputStream.flush();
    }

    public static Message receiveMessage(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException{
        Message message = (Message)objectInputStream.readObject();
        return message;
    }
}
